import { computed } from 'vue'
import type { Rule } from 'ant-design-vue/es/form'
import type { CreateMenu } from '@/types/api/system/menu'
import { MenuType } from '@/types/api/system/menu'

/**
 * 菜单表单验证规则
 * @param form 表单数据
 * @returns 表单验证规则
 */
export const useFormRules = (form: CreateMenu) => {
  const { t } = useI18n()

  return computed<Record<string, Rule[]>>(() => ({
    name: [
      { required: true, message: t('menu.rule.nameRequired'), trigger: 'blur' },
      { min: 2, max: 50, message: t('menu.rule.nameLength'), trigger: 'blur' }
    ],
    type: [
      { required: true, message: t('menu.rule.typeRequired'), trigger: 'change' }
    ],
    orderNum: [
      { required: true, message: t('menu.rule.orderNumRequired'), trigger: 'blur' },
      { type: 'number', message: t('menu.rule.orderNumType'), trigger: 'blur' }
    ],
    path: form.type !== MenuType.BUTTON ? [
      { required: true, message: t('menu.rule.pathRequired'), trigger: 'blur' },
      { max: 200, message: t('menu.rule.pathLength'), trigger: 'blur' }
    ] : [],
    component: form.type === MenuType.MENU && !form.isExternal ? [
      { required: true, message: t('menu.rule.componentRequired'), trigger: 'blur' },
      { max: 200, message: t('menu.rule.componentLength'), trigger: 'blur' }
    ] : [],
    permission: form.type !== MenuType.DIRECTORY ? [
      { max: 100, message: t('menu.rule.permissionLength'), trigger: 'blur' }
    ] : []
  }))
} 